package com.yycx.module.bus.provider.service.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.BusSalemanBank;
import com.yycx.module.bus.provider.mapper.BusSalemanBankMapper;
import com.yycx.module.bus.provider.service.BusSalemanBankService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 业务员银行账户接口实现类
 *
 * @author flyme
 * @date 2020-07-31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusSalemanBankServiceImpl extends BaseServiceImpl<BusSalemanBankMapper, BusSalemanBank> implements BusSalemanBankService {

    @Autowired
    @Lazy
    private BusSalemanService salemanService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusSalemanBank bsb, EntityMap extra) {
        bsb.setBankState(CommonConstants.ENABLED);
        Long saleManId = bsb.getSaleManId();
        BusSaleman saleman = salemanService.getById(saleManId);
        bsb.setDeptId(saleman.getDeptId());
        if(FlymeUtils.isEmpty(bsb.getSaleManName())){
            bsb.setSaleManName(saleman.getSaleManName());
        }
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusSalemanBank> cq, BusSalemanBank bsb, EntityMap requestMap) {
        Long saleManId = bsb.getSaleManId();
        cq.eq(true, "saleManId", saleManId);
        cq.orderByDesc("bsb.createTime");
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeListEntityMap(CriteriaQuery<BusSalemanBank> cq, BusSalemanBank salemanBank, EntityMap requestMap) {
        cq.eq(BusSalemanBank.class, "deptId");
        cq.eq(BusSalemanBank.class, "saleManId");
        cq.eq(true, "bankState", CommonConstants.ENABLED);
        return super.beforeListEntityMap(cq, salemanBank, requestMap);
    }


    @Override
    public ResultBody deleteBySaleManId(Serializable saleManId) {
        CriteriaDelete cd = new CriteriaDelete();
        cd.eq(true, "saleManId", saleManId);
        Boolean result = remove(cd);
        return ResultBody.ok("删除成功", result);
    }
}
